
import { isvalidUsername } from '@/utils/validate'
import {updatePwdNoLogin} from '@/api/user'
import LangSelect from '@/components/LangSelect'
export default {
  name: 'login',
  components: { LangSelect },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error(this.$t('login.errorAccount') ))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error(this.$t('login.errorPassword')))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      pwdType: 'password',
      redirect:'/',
      pwdModifyVisible: false,
      pwdChangeLoading: false,
      pwdForm: {
        oldPwd: '',
        newPwd: '',
        confPwd: ''
      },
      pwdRules: {
        oldPwd: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        confPwd: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
      },

    }
  },
  mounted(){
    this.init()
  },
  methods: {
    init(){
      let redirect = this.$route.query.redirect
      if(redirect){
        this.redirect = redirect
      }
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push(this.redirect)

          }).catch((err) => {
            if(err.code === 8000){
              this.pwdModifyVisible = true;
              this.pwdForm.newPwd = "";
              this.pwdForm.oldPwd = "";
              this.pwdForm.confPwd = "";
            }
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    handleChangePwd() {
      this.$refs.pwdForm.validate(valid => {
        if(this.pwdForm.confPwd !== this.pwdForm.newPwd){
          this.$message({
            showClose: true,
            message: '两次输入密码不一致，请重新输入',
            type: 'warning'
          });
          return
        }
        if(this.pwdForm.oldPwd === this.pwdForm.newPwd){
          this.$message({
            showClose: true,
            message: '新旧密码不可相同,请重新输入',
            type: 'warning'
          });
          return
        }
        if (valid) {
          console.log(this.pwdForm)
          const params = {
            oldPassword : this.pwdForm.oldPwd,
            password : this.pwdForm.newPwd,
            userName : this.loginForm.username
          }
          this.pwdChangeLoading = true;
          updatePwdNoLogin(params).then((res) => {
            this.pwdForm.newPwd = "";
            this.pwdForm.confPwd = "";
            this.pwdForm.oldPwd = "";
            this.pwdModifyVisible = false;
            this.$notify({
              title: '提示',
              message: '密码修改成功，请重新登陆！',
              type: 'warning',
              duration: 5000
            })
          }).finally(() =>{
            this.pwdChangeLoading = false;
          })
        }
      })
    },
    handleCancelPwd(){
      this.pwdForm.newPwd = "";
      this.pwdForm.confPwd = "";
      this.pwdForm.oldPwd = "";
      this.pwdModifyVisible = false;
      this.getCode();
    },
  }
}
